//
//  PosterView.m
//  Movie
//
//  Created by tengyang on 15/7/24.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "PosterView.h"
#import "UIViewExt.h"
#import "PosterCollectionView.h"
#import "movieModal.h"
#import "common.h"

#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height




@implementation PosterView

- (id)initWithFrame:(CGRect)frame
{
    self=[super initWithFrame:frame];
    if (self) {
        
        
        [self _createPosterView];
        [self _createCoverView];
        [self _createHeadView];
        [self _createBottomTitleLable];
        [self _createSlipeGesture];
        
        [_indexCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];
        [_posterCollectionView addObserver:self forKeyPath:@"currentIndex" options:NSKeyValueObservingOptionNew context:nil];

    }
    return self;
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    NSNumber *number = [change objectForKey:@"new"];
    
    NSInteger index = [number integerValue];
    
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:index inSection:0];
    
    //小海报动了
    if ([object isKindOfClass:[IndexCollectionView class]] && _posterCollectionView.currentIndex != index) {
        
        [_posterCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        _posterCollectionView.currentIndex = index;
        
        
    }else if([object isKindOfClass:[PosterCollectionView class]] && _indexCollectionView.currentIndex!= index){
        
        [_indexCollectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
        
        _indexCollectionView.currentIndex = index;
    }
    //更新 标题
    movieModal *modal = _movieModalArray[index];
    _bottomTitleLable.text = modal.title;
}


- (void)_createSlipeGesture
{
    UISwipeGestureRecognizer *swipeDown=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDownAction)];
    swipeDown.direction=UISwipeGestureRecognizerDirectionDown;
    [self addGestureRecognizer:swipeDown];
}

- (void)swipeDownAction
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    UIButton *buttom=(UIButton *)[_headView viewWithTag:100];
    _headView.top=0;
    _coverView.hidden=NO;
    buttom.selected=!buttom.selected;
    [UIView commitAnimations];
}

- (void)_createHeadView
{
    
    _headView=[[UIView alloc]initWithFrame:CGRectMake(0, -100, kwidth, 136)];
    _headView.backgroundColor=[UIColor clearColor];
    [self addSubview:_headView];
    
    
    UIImage *image=[UIImage imageNamed:@"indexBG_home"];
    image=[image stretchableImageWithLeftCapWidth:0 topCapHeight:1];
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:_headView.bounds];
    imageView.image=image;
    [_headView addSubview:imageView];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    
    _indexCollectionView = [[IndexCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 100) collectionViewLayout:layout];
    _indexCollectionView.backgroundColor=[UIColor yellowColor];
    _indexCollectionView.itemWidth = kWidth/5;
    
    
    [_headView addSubview:_indexCollectionView];
    
    
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake((kwidth-20)/2, 110, 20, 20)];
    //button.backgroundColor=[UIColor orangeColor];
    button.tag=100;
    [button setImage:[UIImage imageNamed:@"down_home"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"up_home"] forState:UIControlStateSelected];
    
    [button addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:button];
    
    [self addSubview:_headView];
}

- (void)pushAction:(UIButton *)button
{
    
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    if (button.selected==NO) {
        _headView.top=0;
        _coverView.hidden=NO;
    }
    else{
    _headView.top=-100;
        _coverView.hidden=YES;
    }
    
    button.selected=!button.selected;
    [UIView commitAnimations];
}

- (void)_createCoverView
{
    _coverView=[[UIControl alloc]initWithFrame:self.bounds];
    _coverView.backgroundColor=[UIColor colorWithWhite:0.8 alpha:0.8];
    _coverView.hidden=YES;
    [_coverView addTarget:self action:@selector(coverHidden) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_coverView];
    
}
- (void)coverHidden
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    UIButton *button=(UIButton *)[_headView viewWithTag:100];
    _headView.top=-100;
    _coverView.hidden=YES;
    button.selected=!button.selected;
    [UIView commitAnimations];
    
}



- (void)_createPosterView
{
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    
    layout.scrollDirection=UICollectionViewScrollDirectionHorizontal;
    layout.minimumInteritemSpacing=0;
    layout.minimumLineSpacing=0;
    _posterCollectionView=[[PosterCollectionView alloc]initWithFrame:CGRectMake(0, 36, kwidth, kheight-100-35-49) collectionViewLayout:layout];
    _posterCollectionView.backgroundColor=[UIColor yellowColor];
    [self addSubview:_posterCollectionView];
    
    
    
}
- (void)setMovieModalArray:(NSArray *)movieModalArray{
    
    _movieModalArray = movieModalArray;
    _posterCollectionView.movieModalArray = movieModalArray;
    _indexCollectionView.movieModalArray=movieModalArray;
    movieModal *modal=_movieModalArray[0];
    _bottomTitleLable.text=modal.title;
    
}


- (void)_createBottomTitleLable
{
    
    
    _bottomTitleLable=[[UILabel alloc]initWithFrame:CGRectMake(0, kheight-35-49-64, kwidth, 35)];
    _bottomTitleLable.backgroundColor=[UIColor grayColor];
    movieModal *modal=_movieModalArray[0];
    _bottomTitleLable.text=modal.title;
    _bottomTitleLable.textColor=[UIColor whiteColor];
    _bottomTitleLable.textAlignment=NSTextAlignmentCenter;
    
    
    
    [self addSubview:_bottomTitleLable];
}

@end
